package com.meepo.framework.design.helper

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.annotation.FloatRange

/**
 * @author
 *
 * @date  2020/11/1 8:59 PM
 * @version 1.0
 */
object StatusBarHelper {

    fun immerse(activity: Activity, topView: View) {
        immerse(activity, topView, false)
    }

    fun immerse(activity: Activity, topView: View, dark: Boolean) {

        immerse(activity)
        setMargin(activity, topView)

        if (dark) setDarkMode(activity)
    }

    fun immerse(activity: Activity) {

        val window = activity.window


        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = mixColor(0, 0f)

        val systemUiVisibility = window.decorView.systemUiVisibility

        window.decorView.systemUiVisibility = systemUiVisibility or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }

    fun setDarkMode(activity: Activity) {
        setMode(activity, true)
    }

    internal fun setMode(activity: Activity, dark: Boolean) {

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {

            var systemUiVisibility = activity.window.decorView.systemUiVisibility

            systemUiVisibility = if (dark) {
                systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
            }

            activity.window.decorView.systemUiVisibility = systemUiVisibility
        } else {
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            createTranslucentStatusBar(activity.window.decorView as ViewGroup, 0, 0f)
        }
    }


    fun setMargin(context: Context, topView: View) {

        val statusBarHeight = getHeight(context)

        val lp = topView.layoutParams

        // 增加View的高度
        if (lp != null && lp.height > 0) {
            lp.height += statusBarHeight
        }

        // 增加View paddingTop
        topView.setPadding(
                topView.paddingLeft,
                topView.paddingTop + statusBarHeight,
                topView.paddingRight,
                topView.paddingBottom
        )
    }

    // 获取状态栏高度
    fun getHeight(context: Context): Int {
        var result = 24
        val resId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        result = if (resId > 0) {
            context.resources.getDimensionPixelSize(resId)
        } else {
            TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    result.toFloat(), Resources.getSystem().displayMetrics
            ).toInt()
        }
        return result
    }

    private fun createTranslucentStatusBar(
            container: ViewGroup,
            color: Int,
            @FloatRange(from = 0.0, to = 1.0) alpha: Float
    ) {
        val mixtureColor = mixColor(color, alpha)
        var translucentView: View? = container.findViewById(android.R.id.custom)
        if (translucentView == null && mixtureColor != 0) {
            translucentView = View(container.context)
            translucentView.id = android.R.id.custom
            val lp = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    getHeight(container.context)
            )
            container.addView(translucentView, lp)
        }
        translucentView?.setBackgroundColor(mixtureColor)
    }

    private fun mixColor(color: Int, @FloatRange(from = 0.0, to = 1.0) alpha: Float): Int {
        val a = if (color and -0x1000000 == 0) 0xff else color.ushr(24)
        return color and 0x00ffffff or ((a * alpha).toInt() shl 24)
    }

}